package admin.controllers;

import helpers.CookieHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import admin.models.ApplicationModel;

import entities.Application;


public class ApplicationProcessServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String strIds = (String) request.getParameter("ids");
	    String action = (String) request.getParameter("action");
	    int status = 0;
	    String[] ids = strIds.split(",");
	    ApplicationModel appModel = ApplicationModel.getInstance();
	    
	    ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("status_active", "status_inactive"));
	    
	    for (int i = 0; i < ids.length; i++) {
	        if (!ids[i].isEmpty()) {
	        	if(statusAction.contains(action.toLowerCase())) {
	                if (action.equalsIgnoreCase("status_active")) {
	                	status = 1;
					} else if (action.equalsIgnoreCase("status_inactive")) {
						status = 0;
					}

	                Application app = appModel.get(URLDecoder.decode(ids[i]));
	                if (app != null) {
	                	app.setStatus(status);
	                	appModel.insert(app);
	        		}
	        	} 
	        }
	    }
	    
	    CookieHelper.setCookie(response, "success", "You change success appplcation " + strIds);
	}
}
